<?php defined("MAIN_DIR") or die("No direct access");

if ($route->GET('type') != "")
{
	$search = UV::readMQ($route->GET('s'));
	switch ($route->GET('type')) {
		case "city":
			$query = "select `city_id` as `id`,`city_name` as `name`,`path` from `city` where `city_name` like '%$search%' order by `name`"; break;
		case "bank":
			$query = "select `B`.`bank_id` as `id`,`B`.`link`,`B`.`name`,`B`.`parent`,`C`.`path` from `bank` as `B`,`city` as `C` where `B`.`name` like '%$search%' and `B`.`city_id` = '$_SESSION[city_admin]' and `B`.`city_id` = `C`.`city_id` order by `name`";
			break;
		case "deposit":
			$query = "select `D`.`deposit_id` as `id`,`D`.`name`,`C`.`path` from `deposit` as `D`,`bank` as `B`,`city` as `C` where `D`.`name` like '%$search%' and `D`.`bank_id` = `B`.`bank_id` and `B`.`city_id` = '$_SESSION[city_admin]' and `B`.`city_id` = `C`.`city_id` order by `name`";
			break;
		case "home":
			$query = "select `H`.`home_id` as `id`,`H`.`name`,`C`.`path` from `home` as `H`,`bank` as `B`,`city` as `C` where `H`.`name` like '%$search%' and `H`.`bank_id` = `B`.`bank_id` and `B`.`city_id` = '$_SESSION[city_admin]' and `B`.`city_id` = `C`.`city_id` order by `name`";
			break;
		case "auto":
			$query = "select `A`.`auto_id` as `id`,`A`.`name`,`C`.`path` from `auto` as `A`,`bank` as `B`,`city` as `C` where `A`.`name` like '%$search%' and `A`.`bank_id` = `B`.`bank_id` and `B`.`city_id` = '$_SESSION[city_admin]' and `B`.`city_id` = `C`.`city_id` order by `name`";
			break;
		case "needs":
			$query = "select `Cr`.`credit_id` as `id`,`Cr`.`name`,`C`.`path` from `credit` as `Cr`,`bank` as `B`,`city` as `C` where `Cr`.`name` like '%$search%' and `Cr`.`bank_id` = `B`.`bank_id` and `B`.`city_id` = '$_SESSION[city_admin]' and `B`.`city_id` = `C`.`city_id` order by `name`";
			break;
		case "carts":
			$query = "select `Cr`.`cart_id` as `id`,`Cr`.`name`,`C`.`path` from `cart` as `Cr`,`bank` as `B`,`city` as `C` where `Cr`.`name` like '%$search%' and `Cr`.`bank_id` = `B`.`bank_id` and `B`.`city_id` = '$_SESSION[city_admin]' and `B`.`city_id` = `C`.`city_id` order by `name`";
			break;
		case "business":
			$query = "select `Bs`.`business_id` as `id`,`Bs`.`name`,`C`.`path` from `business` as `Bs`,`bank` as `B`,`city` as `C` where `Bs`.`name` like '%$search%' and `Bs`.`bank_id` = `B`.`bank_id` and `B`.`city_id` = '$_SESSION[city_admin]' and `B`.`city_id` = `C`.`city_id` order by `name`";
			break;
		case "vacancy":
			$query = "select `V`.`vacancy_id` as `id`,`V`.`title` as `name`,`C`.`path` from `vacancy` as `V`,`bank` as `B`,`city` as `C` where `V`.`title` like '%$search%' and `V`.`bank_id` = `B`.`bank_id` and `B`.`city_id` = '$_SESSION[city_admin]' and `B`.`city_id` = `C`.`city_id` order by `name`";
			break;
	}
	$res = mysql_query($query);
	if (mysql_num_rows($res) > 0) {
		
		$str = "{\"search\":[";
		$i = 0;
		
		while($row = mysql_fetch_array($res)) {
			
			$i++;
			$str .= "{\"id\":$row[id],\"name\":\"".UV::readMQ($row['name'])."\",\"city\":\"$row[path]\"";
			if ($_GET['type'] == "bank")
				$str .= ",\"parent\":\"".($row['parent'] ? "Филиал" : "Гол. офис")."\",\"link\":\"".$row['link'].".html\"";
			$str .= "}";
			
			if ($i <= mysql_num_rows($res)-1)
				$str .= ",";
		}
		echo $str .= "]}";
	}
}

?>